<?php

/**
 * Retrieves a module bootstrap instance from the initializer plugin.
 *
 * Use this if you want to invoke a method defined by a module bootstrap from
 * a controller or view script.
 *
 * This is automatically registered as an action and view helper by
 * Mz_Module_Initializer.
 *
 * @author     Mon Zafra <monzee at gmail>
 * @copyright  (c)2009 Mon Zafra
 * @category   Mz
 * @package    Mz_Module
 * @subpackage Helper
 * @license    http://mz-project.googlecode.com/svn/trunk/LICENSE    MIT License
 * @version    SVN: $Id: Module.php 6 2009-02-20 11:44:33Z monzee $
 */
class Mz_Module_Helper_Module extends Zend_Controller_Action_Helper_Abstract
{
    /**
     * Gets a module instance from module initializer.
     *
     * @param string $module module name
     * @return mixed The module bootstrap instance, false if not found, the
     *               module initializer plugin if no parameters are passed
     */
    public function direct($module = null)
    {
        $module = $this->_normalizeModuleName($module);
        $plugin = $this->getFrontController()->getPlugin('Mz_Module_Initializer');

        if ('' == $module) {
            return $plugin;
        }

        return $plugin->getModule($module);
    }

    /**
     * Doubles as a view helper, proxy to the above method
     * 
     * @param string $module 
     * @return mixed The module bootstrap instance, false if not found, the
     *               module initializer plugin if no parameters are passed
     */
    public function module($module = null)
    {
        return $this->direct($module);
    }

    protected function _normalizeModuleName($module)
    {
        return strtolower($module);
    }
}
